/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viajaconmigo.web.validators;

import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Pablo
 */
@FacesValidator (value="numDiasValidator")
public class numDiasValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) 
            throws ValidatorException {
        //String attribute = (String)component.getAttributes().get("ndias");
        String validar = context.getExternalContext().getRequestParameterMap().get("validar");
        if (validar != null && !validar.equals("false")) {
            //Integer atInt = (Integer)component.getAttributes().get("ndias");
            //Short at = atInt.shortValue();
            Short at = (Short) component.getAttributes().get("ndias");
            Integer attribute;
            if (at != null)
                attribute = new Integer ((Short)at);
            else
                attribute = null;
            //String attribute = value.toString();
            //String attribute = attribute2.toString();
            /*
            Integer ndiasmin;
            try{
                ndiasmin = new Integer(attribute.substring(0, attribute.indexOf("-")));
            }catch(Exception e){
                ndiasmin = null;
            }
            Integer ndiasmax;
            try{
                ndiasmax = new Integer(attribute.substring(attribute.indexOf("-")+1, attribute.length()));
            }catch(Exception e){
                ndiasmax = null;
            }
            * *
            */
            if ((value == null && attribute != null) || (value != null && attribute == null)){
                FacesMessage message = new FacesMessage();
                message.setSummary("No se puede especificar sólo un límite de días");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }        
            if ((value != null && attribute != null) && (attribute.compareTo(new Integer((Short)value))>0)){
                FacesMessage message = new FacesMessage();
                message.setSummary("El número de días de la izquierda debe ser menor que el de la derecha");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }
            if (validar.equals("new") && value == null && attribute == null){
                FacesMessage message = new FacesMessage();
                message.setSummary("Se debe especificar un número de días");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }
        }
    }
    
}
